<?php

// +----------------------------------------------------------------------
// | EasyAdmin
// +----------------------------------------------------------------------
// | PHP交流群: 763822524
// +----------------------------------------------------------------------
// | 开源协议  https://mit-license.org
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zhongshaofa/EasyAdmin
// +----------------------------------------------------------------------

namespace app\admin\service;

use app\admin\model\Agent;
use app\admin\model\Treepaths;
use think\facade\Cache;

/**
 * 树状结构
 * @package app\admin\service
 */
class TreeService
{
    private static function getModel(){
        return new Treepaths();
    }
    public static function buildMenuChild($pid, $menuList)
    {
        $treeList = [];
        foreach ($menuList as &$v) {

            if ($pid == $v['pid'] ) {
                $node = $v;
                $child = self::buildMenuChild($v['id'], $menuList);
                if (!empty($child)) {
                    $node['children'] = $child;
                    $node['isParent'] = true;

                }

                    $treeList[] = $node;

            }
        }
        return $treeList;
    }
    /**
     * 获取直属下级
     * @param $cid
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getChild($pid)
    {
        $model=self::getModel();
        $row= $model->where('pid',$pid)->where('distance','1')->with('child')->find();
        if(empty($row)){
            return null;
        }
        return $row['child'];

    }
    /**
     * 获取直属上级
     * @param $cid
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getParent($cid)
    {
        $model=self::getModel();
        $row= $model->where('cid',$cid)->where('distance','1')->with('parent')->find();
      if(empty($row)){
          return null;
      }
      return $row['parent'];

    }

    /**
     * 获取所有子孙
     * @param $pid int 上级id
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getAllChildren($pid)
    {
        $model=self::getModel();
        $list= $model->where('pid',$pid)->where('cid','<>',$pid)->with('child')->select();
        $arr=[];
        foreach ($list as $item) {

            $arr[]=$item['child'];
        }
        return $arr;
    }
    /**
     * 获取所有子孙id
     * @param $pid int 上级id
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getAllChildrenId($pid)
    {
        $model=self::getModel();
        $list= $model->where('pid',$pid)->where('cid','<>',$pid)->column('cid');

        return $list;
    }

    /**
     * 获取所有上级
     * @param $cid int 下级id
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getAllParent($cid)
    {
        $model=self::getModel();
        $arr=[];

        $list= $model->where('cid',$cid)->where('pid','<>',$cid)->with('parent')->select();
        foreach ($list as $item) {

            $arr[]=$item['parent'];
        }
        return $arr;
    }

    /**
     * 添加自己节点
     */
    public static function addSelf($cid)
    {
        $model=self::getModel();

        $model->save([
            'pid'=>$cid,
            'cid'=>$cid,
            'distance'=>0,
        ]);
    }
    /**
     * 添加下级
     */
    public static function addChild($pid,$cid,$is_self=false)
    {
        $model=self::getModel();
        $parents= $model->where('cid',$pid)->where('distance','<>',0)->select();

        $list=[];
//        if($is_self){
//            $list[]=[
//                'pid'=>$cid,
//                'cid'=>$cid,
//                'distance'=>0,
//            ];
//        }
        $list[]=[
                'pid'=>$pid,
                'cid'=>$cid,
                'distance'=>1,
            ];
        foreach ($parents as $parent) {
            $list[]=['pid'=>$parent['pid'],'cid'=>$cid,'distance'=>$parent['distance']+1];
        }
        $model->saveAll($list);
    }

    /**
     * 关联删除
     * @param array $ids
     */
    public static function relateDelete($ids)
    {
        $model=self::getModel();
        $agent_ids=   $model->whereIn('pid',$ids)->column('cid');

        if(!empty($agent_ids)){
            $ids=   array_merge($ids,$agent_ids);
        }
        $model->whereIn('cid',$ids)->delete();

       // Agent::destroy($agent_ids);
       // Agent::whereIn('id',$agent_ids)->delete();

    }
}
